/* eslint-disable no-undef */
<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <template #title>
        <span class="custom-title">我的频道</span>
        <!-- <van-tag type="danger">标签</van-tag> -->
      </template>
      <van-button class="edit-btn" round type="danger" plain size="mini" @click="isEdit=!isEdit">{{isEdit? '完成':'编辑'}}</van-button>
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item @click="onMyChannelClick(channel, index)" class="grid-item" v-for="(channel,index) in myChannels" :key="index">
        <van-icon slot="icon" name="clear" v-show="isEdit && !fiexChannels.includes(channel.id)"></van-icon>
        <span class="text" slot="text" :class="{active: index === active}">{{channel.name}}</span>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
    <van-cell :border="false">
      <template #title>
        <span class="custom-title">频道推荐</span>
        <!-- <van-tag type="danger">标签</van-tag> -->
      </template>

    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item icon="plus" @click="onAddChannels(channel)" class="grid-item" v-for="(channel, index) in recommendChannels" :key="index" :text="channel.name" />
    </van-grid>
    <!-- / 频道编辑 -->
  </div>
</template>

<script>
import { getAllCHannels, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  data() {
    return {
      allChannels: [], // 所有频道
      myChannel: [], // 存储我的频道
      isEdit: false, // 控制编辑状态的显示
      fiexChannels: [0] // 固定频道 不允许删除
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels() {
      const channels = [] // 存储推荐频道的数组
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(myChannels => {
          return myChannels.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    }
  },
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  created() {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels() {
      try {
        const { data } = await getAllCHannels()
        this.myChannel = this.myChannels
        this.allChannels = data.data.channels
        // console.log(1)
        // console.log(data)
      } catch (err) {
        this.$toast('数据获取失败')
        console.log(err)
      }
    },
    async onAddChannels(channel) {
      // console.log(channel)
      this.myChannel.push(channel)
      if (this.user) {
        // 已登录
        try {
          const { data } = await addUserChannel({
            id: channel.id,
            seq: this.myChannel.length
          })
          console.log(data)
        } catch (err) {
          this.$toast('添加失败')
        }
      } else {
        // 未登录
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    },

    // 删除频道
    onMyChannelClick(channel, index) {
      if (this.isEdit) {
        // 如果是固定频道 则不能删除，直接返回
        if (this.fiexChannels.includes(channel.id)) {
          return
        }
        // 如果是编辑状态，执行删除操作
        // console.log(index)
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.myChannel.splice(index, 1)
        // 处理持久化
        this.deleteChannel(channel)
      } else {
        // 如果是非编辑状态，则进行切换频道
        // 使用父子组件通信的方法进行传值  就是子组件向父组件传值
        this.$emit('update-active', index, false)
      }
    },
    async deleteChannel(channel) {
      if (this.user) {
        // 已登录，则将数据更新到线上
        try {
          await deleteUserChannel(channel.id)
        } catch (err) {
          this.$toast('删除失败')
        }
      } else {
        // 未登录，则将数据存储到本地
        setItem('TOUTIAO_CHANNELS', this.myChannel)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding: 85px 0;
  .custom-title {
    font-size: 32px;
    color: #333333;
  }
  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }
  .grid-item {
    width: 160px;
    height: 86px;
    // background-color: #f4f5f6;
    /deep/.van-grid-item__content {
      white-space: nowrap;
      background-color: #ddd;
      // font-size: 28px;
      // color: #222222;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }
  /deep/.my-grid {
    .grid-item {
      .van-icon {
        position: absolute;
        top: -10px;
        right: -10px;
        font-size: 30px;
        z-index: 2;
        color: #cacaca;
      }
    }
  }
  /deep/.recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        // justify-content: center;
        // align-items: center;
        .van-icon {
          font-size: 28px;
          margin-right: 6px;
        }
        .van-grid-item__text {
          margin-top: 0;
        }
      }
    }
  }
}
</style>
